home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c
- Subject: Re: Pointers to structures
- Date: 3 Jan 1996 06:31:23 GMT
- Organization: Kalevi, Inc
- Message-ID: <4cd7rr$aae@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Jan 02, 1996 21:59:00 in article <Pointers to structures>,
- 'icarus@loomis (Tel Janin Aellinsar)' wrote:
-
-
- >I'm having trouble using a pointer to a struct. The idea is to have a
- >struct get filled by a function, which gets the address and everything
- >via function(struct type*). Very straightforward. However, if I try to
- >CHANGE anything in the struct, it just goes back when the function is
- >over. So, let's pretend this imaginary structure is what I'm using:
- >
- >struct st1 {
- >char *name;
- >int yadda;
- >};
- >
- >And the function is:
- >
- >fn1(struct st1 *st)
- >{
- >st = (struct st1*)malloc(sizeof(struct st1*));
-
- You're allocating memory for a pointer (4 bytes on most
- machines). Change it to:
- st = malloc(sizeof(struct st1));
-
- (Note: I dropped the cast also as it shouldn't be necessary).
-
- --
-
- Pete
-
-
-
-
-
-